package it.apiconsulting.live.controller;


import it.apiconsulting.live.model.Noleggio;
import it.apiconsulting.live.util.HibernateUtil;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;


public class NoleggioManager extends HibernateUtil {

	public Noleggio add(Noleggio noleggio) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(noleggio);
		session.getTransaction().commit();
		return noleggio;
	}
	public Noleggio delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Noleggio noleggio = (Noleggio) session.load(Noleggio.class, id);
		if(null != noleggio) {
			session.delete(noleggio);
		}
		session.getTransaction().commit();
		return noleggio;
	}

	public List<Noleggio> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Noleggio> noleggi = null;
		try {
			
			Query query = session.createQuery("from Noleggio");
			noleggi = (List<Noleggio>)query.list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return noleggi;
	}
}
